using Reflector;
using Reflector.CodeModel;
using System;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using FileGenerator.AddIn.UI;

namespace FileGenerator.AddIn
{ 
	public class FileGeneratorPackage : IPackage
	{
		private const Keys MenuKeys = Keys.Control | Keys.Shift | Keys.R;
		private const string ToolsCommandBar = "Tools";
        private const string ToolsTitle = "&Reflector.FileGenerator...";
		private const string WindowID = "FileGeneratorWindow";
		private const string WindowTitle = "Reflector.FileGenerator(Revised)";

		private ICommandBarButton fileGeneratorCommandBarButton;
		private ICommandBarSeparator fileGeneratorCommandBarSeparator;
		private IWindow fileGeneratorWindow;
		static private IServiceProvider serviceProvider;
		private IWindowManager windowManager;
        static private IConfigurationManager configManager;

		public FileGeneratorPackage()
			: base()
		{
		}

        public static string GetConfigString( string section,string key )
        {
            //"AssemblyBrowser""Visibility"
            IConfiguration cfg = configManager[section];
            return cfg.GetProperty(key);
        }

		public void Load(IServiceProvider serviceProvider)
		{
            String s1 = new String('0',0);
			serviceProvider = serviceProvider;

			var fileGeneratorControl = new FileGeneratorControl(serviceProvider);

            configManager = (IConfigurationManager)serviceProvider.GetService(typeof(IConfigurationManager));
           

            System.Console.Write(s1);

			windowManager =
				 (IWindowManager)serviceProvider.GetService(typeof(IWindowManager));
			windowManager.Windows.Add(WindowID, fileGeneratorControl, WindowTitle);

			fileGeneratorWindow = windowManager.Windows[WindowID];
			fileGeneratorWindow.Content.Dock = DockStyle.Fill;

			var commandBarManager =
				 (ICommandBarManager)serviceProvider.GetService(typeof(ICommandBarManager));
			var commandToolItems = commandBarManager.CommandBars[ToolsCommandBar].Items;
			fileGeneratorCommandBarSeparator = commandToolItems.AddSeparator();
			fileGeneratorCommandBarButton = commandToolItems.AddButton(
				 ToolsTitle, new EventHandler(this.OnFileGeneratorButtonClick), MenuKeys);
		}

		private void OnFileGeneratorButtonClick(object sender, EventArgs e)
		{
			this.fileGeneratorWindow.Visible = true;
		}

		public void Unload()
		{
			var commandBarManager =
				(ICommandBarManager)serviceProvider.GetService(typeof(ICommandBarManager));
			var commandToolItems = commandBarManager.CommandBars[ToolsCommandBar].Items;

			commandToolItems.Remove(this.fileGeneratorCommandBarButton);
			commandToolItems.Remove(this.fileGeneratorCommandBarSeparator);
		}
	}
}